<? 
class uaheader {
    
    public $type;
    public $browser;
    public $header;
    
    /**
     * 构造函数
     * @param string $type       平台类型
     * @param string $browser    浏览器
     */
    function __construct($type = null , $browser = null){
        $function = [
            'pc'  => 'getPCHeader',
            'wap' => 'getWapHeader',
        ];
        
        //指定浏览器
        $this->browser = $browser;
        
        //指定平台
        $this->type = $type? :array_rand($function);
        
        $this->$function[$this->type]();
    }
    
    //取PC 请求头
    public function getPCHeader(){
        $function = [
            'chrome' => 'getpcchrome',
            '360'    => 'getpc360',
            'baidu'  => 'getpcbaidu',
            'firefox'=> 'getpcfirefox',
        ];
        
        //未指定浏览器,随机一个
        $this->browser = $this->browser? :array_rand($function); 
        
        $function = $function[$this->browser];
        $this->header  = eval('return '.$function."();");
    }
    
    //取Wap 请求头
    public function getWapHeader(){
        $function = [
            'uc'    => 'getuc',
            'qq'    => 'getqq',
            '360'   => 'get360',
            'sogou' => 'getsogou',
            'chrome' => 'getchrome',
            'liebao' => 'getliebao',
        ];
        
        //未指定浏览器,随机一个
        $this->browser = $this->browser? :array_rand($function); 
        
        //随机版本和固件号
        list($androidv , $firmware) = getandroidv();
        
        $function = $function[$this->browser];
        
        $this->header  = eval('return '.$function."('$androidv','$firmware');");
    }
    
}
//=========================================以下不属于类方法==============================

/**
 * 搜狗wap浏览器请求头
 * @param string $androidv   安卓版本
 * @param string $firmware   安卓版本对应的固件版本
 */
function getsogou($androidv , $firmware){
    return array(
        "Connection: keep-alive",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Upgrade-Insecure-Requests: 1",
        "sogouidcrypt: uX5DdRE21RPfrRXV2S7TvRYTd3gYF57jMbaFyHQQmylpPDvrm/ifAneBzNM0T4If",//??
        "Accept-Language: zh-CN,en-US;q=0.8",
        "Accept-Encoding: gzip, deflate",
        'User-Agent' => "Mozilla/5.0 (Linux; Android $androidv; ".getphonev()."; Build/$firmware; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/".getchromev()." Mobile Safari/537.36 SogouMSE,SogouMobileBrowser/5.5.5",
    );
}
//火狐wap
function getpcfirefox(){
    $version = [
        '50.1.0',
        '51.0.1',
        '51.0',
        '52.0',
        '53.0b1',
        '53.0b2',
        '53.0b3',
        '6.0.1',
        '6.0.2',
        '6.0',
        '7.0.1',
        '7.0',
        '8.0.1',
        '8.0',
        '9.0.1',
        '9.0',
    ];
    $version = $version[array_rand($version)];
    return array(
        "Connection: keep-alive",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Upgrade-Insecure-Requests: 1",
        "sogouidcrypt: uX5DdRE21RPfrRXV2S7TvRYTd3gYF57jMbaFyHQQmylpPDvrm/ifAneBzNM0T4If",//??
        "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
        "Accept-Encoding: gzip, deflate, br",
        'User-Agent' => "Mozilla/5.0 (Windows NT ".getwindowsv()."; WOW64; rv:$version) Gecko/20100101 Firefox/$version",
    );
}
//百度wap
function getpcbaidu(){
    $version = [
        '6.5',
        '7.6',
        '8.7',
    ];
    $version = $version[array_rand($version)];
    return array(
        "Connection: keep-alive",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Upgrade-Insecure-Requests: 1",
        "sogouidcrypt: uX5DdRE21RPfrRXV2S7TvRYTd3gYF57jMbaFyHQQmylpPDvrm/ifAneBzNM0T4If",//??
        "Accept-Language: zh-CN,zh;q=0.8",
        "DNT: 1",
        "X-DevTools-Emulate-Network-Conditions-Client-Id: 8F524CD3-0C32-4748-8148-8BD11CB235E7",
        "Accept-Encoding: gzip, deflate",
        'User-Agent' => "Mozilla/5.0 (Windows NT ".getwindowsv()."; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/".getchromev()." BIDUBrowser/$version Safari/537.36",
    );
}
//360wap
function get360($androidv,$firmware){
    return array(
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding: gzip, deflate, sdch",
        "Accept-Language: zh-CN,zh;q=0.8",
        'User-Agent' => "Mozilla/5.0 (Linux; Android $androidv; ".getphonev()." Build/$firmware) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/".getchromev()." Mobile Safari/537.36",
    );
}

//360pc
function getpc360(){
    return array(
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding: gzip, deflate, sdch",
        "Accept-Language: zh-CN,zh;q=0.8",
        'User-Agent' => "Mozilla/5.0 (Windows NT ".getwindowsv()."; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/".getchromev()." Safari/537.36",
    );
}
//QQwap
function getqq($androidv,$firmware){
    $version = [
        '7.3',
        '7.2',
        '7.1',
        '7.0',
    ];
    $version = $version[array_rand($version)];
    return array(
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding: gzip, deflate, sdch",
        "Accept-Language: zh-CN,zh;q=0.8",
        'User-Agent' => "Mozilla/5.0 (Linux; U; Android $androidv; zh-cn; ".getphonev()." Build/$firmware) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/".getchromev()." MQQBrowser/$version Mobile Safari/537.36",
    );
}
//猎豹wap
function getliebao($androidv , $firmware){
    return array(
        "Connection: keep-alive",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding: gzip, deflate",
        "Accept-Language: zh-CN,en-US;q=0.8",
        'User-Agent' => "Mozilla/5.0 (Linux; Android $androidv; ".getphonev()." Build/$firmware) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/".getchromev()." Mobile Safari/537.36 LieBaoFast/4.36.3",
    );
}
//UCwap
function getuc($androidv , $firmware){
    global $ip;
    return array(
        "Connection: keep-alive",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8,UC/145",
        "X-Forwarded-For: $ip",
        "ClientIP: $ip",
        "Accept-Encoding: gzip, deflate, br",
        "Accept-Language: zh-CN,en-US;q=0.8",
        "X-UCBrowser-UA: dv(m1 note);pr(UCBrowser/11.4.2.936);ov(Android 5.1);ss(360*640);pi(1080*1920);bt(YZ);pm(1);bv(1);nm(0);im(0);sr(0);nt(2);",//?????
        'User-Agent' => "Mozilla/5.0 (Linux; U; Android $androidv; zh-CN; ".getphonev()." Build/$firmware) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/".getchromev()." UCBrowser/11.4.2.936 Mobile Safari/537.36",
    );
}
//Chromewap
function getchrome($androidv , $firmware){
    return array(
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding: gzip, deflate, sdch, br",
        "Accept-Language: zh-CN,zh;q=0.8",
        'User-Agent' => "Mozilla/5.0 (Linux; Android $androidv; ".getphonev()." Build/$firmware) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/".getchromev()." Mobile Safari/537.36",
    );
}
//Chromepc
function getpcchrome(){
    return array(
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding: gzip, deflate, sdch, br",
        "Accept-Language: zh-CN,zh;q=0.8",
        'User-Agent' => "Mozilla/5.0 (Windows NT ".getwindowsv()."; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/".getchromev()." Safari/537.36",
    );
}
//随机windows版本号
function getwindowsv(){
    $windows = [
        '5.1',
        '5.2',
        '6.0',
        '6.1',//重复多次加大概率
        '6.1',//重复多次加大概率
        '6.1',//重复多次加大概率
        '6.1',//重复多次加大概率
        '6.1',//重复多次加大概率
        '6.1',//重复多次加大概率
        '6.1',//重复多次加大概率
        '6.2',
        '6.3',
        '6.4',
        '10.0',//重复多次加大概率
        '10.0',//重复多次加大概率
        '10.0',//重复多次加大概率
        '10.0',//重复多次加大概率
        '10.0',//重复多次加大概率
    ];
    return $windows[array_rand($windows)];
}
//随机安卓版本号
function getandroidv(){
    $android = [
        '4.0.4'=>[
            'IMM76B',
        ],
        '4.1.1'=>[
            'JRO03L',
            'JRO03D',
        ],
        '4.1.2'=>[
            'JZO54K',
        ],
        '4.2.1'=>[
            'JOP40D',
        ],
        '4.2.2'=>[
            'JDQ39E',
            'JDQ39',
        ],
        '4.2.3.2'=>[
            'JZO54K',
        ],
        '4.3'=>[
            'JLS36C',
            'JSS15J',
        ],
        '4.4'=>[
            'IMM76D',
        ],
        '4.4.2' =>[
            'KVT49L',
            'KOT49H',
        ],
        '4.4.4' =>[
            'KTU84P',
            'KVT49L',
        ],
        '4.4.5' =>[
            'KOT49H',
        ],
        '5.0.1' =>[
            'LRX22C',
        ],
        '5.0.2' =>[
            'LRX22G',
        ],
    ];
    $key = array_rand($android);
    return array($key,$android[$key][array_rand($android[$key])]);
}
//随机手机型号
function getphonev(){
    $phone = [
        'MI 2',
        'MI 2S',
        'MI 3',
        'MI 4',
        'm1 note',
        'm2 note',
        'Galaxy Nexus',
        'Nexus 7',
        'vivo X3L',
        'vivo X3F',
        'vivo X710L',
        'vivo Xplay3S',
        'vivo Y18L',
    ];
}
//随机Chrom版本
function getchromev(){
    $chrome = [
        '39.0.2171.99','40.0.2214.111','40.0.2214.115','40.0.2214.85','40.0.2214.91','40.0.2214.93','40.0.2214.94','41.0.2272.101','41.0.2272.118','41.0.2272.12','41.0.2272.16','41.0.2272.35','41.0.2272.43','41.0.2272.53','41.0.2272.64','41.0.2272.74','41.0.2272.76','41.0.2272.89','42.0.2282.2','42.0.2283.5','42.0.2288.6','42.0.2290.4','42.0.2291.0','42.0.2292.0','42.0.2293.0','42.0.2294.0','42.0.2295.0','42.0.2296.0','42.0.2297.0','42.0.2298.0','42.0.2299.0','42.0.2300.2','42.0.2301.0','42.0.2302.2','42.0.2303.3','42.0.2304.0','42.0.2305.0','42.0.2305.2','42.0.2305.3','42.0.2307.0','42.0.2311.11','42.0.2311.135','42.0.2311.15','42.0.2311.152','42.0.2311.22','42.0.2311.39','42.0.2311.4','42.0.2311.50','42.0.2311.60','42.0.2311.68','42.0.2311.82','42.0.2311.90','42.0.2313.0','42.0.2315.0','42.0.2317.0','42.0.2319.0','42.0.2321.0','43.0.2323.2','43.0.2327.5','43.0.2328.0','43.0.2330.3','43.0.2333.3','43.0.2334.0','43.0.2337.2','43.0.2342.2','43.0.2344.2','43.0.2346.0','43.0.2347.0','43.0.2350.0','43.0.2351.3','43.0.2355.0','43.0.2357.10','43.0.2357.18','43.0.2357.2','43.0.2357.37','43.0.2357.45','43.0.2357.52','43.0.2357.65','43.0.2357.81','44.0.2358.0','44.0.2359.0','44.0.2364.0','44.0.2367.0','44.0.2369.0','44.0.2371.0','44.0.2376.0','44.0.2383.0','44.0.2384.4','44.0.2391.0','44.0.2392.0','44.0.2398.0','44.0.2403.107','44.0.2403.125','44.0.2403.155','44.0.2403.157','44.0.2403.18','44.0.2403.30','44.0.2403.89','45.0.2414.0','45.0.2418.0','45.0.2421.0','45.0.2454.101','45.0.2454.15','45.0.2454.37','45.0.2454.46','45.0.2454.85','45.0.2454.93','46.0.2467.2','46.0.2468.4','46.0.2471.0','46.0.2478.0','46.0.2482.0','46.0.2486.0','46.0.2490.22','46.0.2490.33','46.0.2490.4','46.0.2490.52','46.0.2490.64','46.0.2490.71','47.0.2503.0','47.0.2506.0','47.0.2508.0','47.0.2513.0','47.0.2522.0','47.0.2526.106','47.0.2526.16','47.0.2526.73','47.0.2526.80','48.0.2527.0','48.0.2536.0','48.0.2564.22','48.0.2564.41','48.0.2564.48','48.0.2564.82','49.0.2578.3','49.0.2587.3','49.0.2589.0','49.0.2593.0','49.0.2607.0','49.0.2614.0','49.0.2623.13','49.0.2623.75','50.0.2628.0','51.0.2704.106','53.0.2785.101','54.0.2802.0','54.0.2840.59','54.0.2840.71','54.0.2840.87','55.0.2857.0','55.0.2883.19','55.0.2883.21','55.0.2883.28','56.0.2895.0','56.0.2896.3','56.0.2902.0','56.0.2906.0'
    ];
    return $chrome[array_rand($chrome)];
}